package com.app.micaihu.view.main.game;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.app.micaihu.R;
import com.app.micaihu.base.BaseOldActivity;
import com.app.micaihu.base.bean.DataBean;
import com.app.micaihu.bean.game.GameScoreBean;
import com.app.micaihu.configure.Config;
import com.app.micaihu.configure.URIContant;
import com.app.micaihu.custom.view.CustomImageView;
import com.app.widget.LoadView;
import com.app.micaihu.custom.view.RatingBarView;
import com.app.micaihu.listener.HttpListener;
import com.app.micaihu.view.main.game.view.GameDescView;
import com.app.micaihu.view.main.game.view.ScorePorgressView;
import com.app.utils.util.StringUtils;
import com.app.utils.util.image.LoadImageUtils;
import com.google.gson.reflect.TypeToken;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Guo on 2017/3/15.
 *
 * 游戏都得分详情
 *
 */

public class GameScoreActivity extends BaseOldActivity implements View.OnClickListener {


    private String gameId;
    private TextView tvGameTitle;
    private TextView tvGameScore;
    private RatingBarView rbvStar;
    private TextView tvCommentCount;
    private LinearLayout starContainer;
    private CustomImageView civHeadPic;
    private String gameTitle;
    private String gameImgUrl;
    private View titlelayout;
    private LoadView loadView2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        if(intent != null){
            gameId = intent.getStringExtra(Config.Extra.PARAMETER1);
            gameTitle = intent.getStringExtra(Config.Extra.PARAMETER2);
            gameImgUrl = intent.getStringExtra(GameDescView.IMAGE_URL);

        }
        addContentView(R.layout.activity_gamescore);

        civHeadPic = (CustomImageView) findViewById(R.id.civ_headpic);
        tvGameTitle = (TextView) findViewById(R.id.tv_game_title);
        tvGameScore = (TextView) findViewById(R.id.tv_game_score);
        rbvStar = (RatingBarView) findViewById(R.id.rb_game_score);
        tvCommentCount = (TextView) findViewById(R.id.tv_game_comment_count);
        starContainer = (LinearLayout) findViewById(R.id.star_container);

        LoadImageUtils.getInstance().loadImage(civHeadPic,gameImgUrl);
        tvGameTitle.setText(gameTitle);


        //设置title
        titlelayout = findViewById(R.id.title);
        titlelayout.setBackgroundColor(getResources().getColor(R.color.transparent));
        ImageView backView = (ImageView) findViewById(R.id.page_head_back_video);
        backView.setImageResource(R.drawable.left_back_click_white_selector);
        View driver = findViewById(R.id.driver_video);
        driver.setVisibility(View.GONE);

        loadView2 = (LoadView) findViewById(R.id.loadview);
        loadView2.setErrorPageClickListener(this);
        loadData();

    }


    /**
     * 请求
     */
    private void loadData() {

        HashMap<String, String> params = new HashMap<>();
        params.put("articleId", gameId);

        HttpListener<DataBean<GameScoreBean>> httpListener = new HttpListener<DataBean<GameScoreBean>>() {
            @Override
            public void onStart() {
                super.onStart();
                loadView2.showLoadPage(null);
            }

            @Override
            public void onError(VolleyError error) {
                loadView2.showErrorPage();
            }

            @Override
            public void onSuccess(DataBean<GameScoreBean> response) {
                if(response.noErrorData()){

                    GameScoreBean gameScoreBean = response.getData();
                    String gradeScore = gameScoreBean.getGradeScore();
                    tvGameScore.setText(gradeScore);
                    String perStartNum = gameScoreBean.getPerStartNum();
                    tvCommentCount.setText(perStartNum+"次评分");

                    rbvStar.setStar(StringUtils.toFloat(gradeScore,0.0f) / 2);
                    //填充分数
                    List<GameScoreBean.Grade> gradeList = gameScoreBean.getGradeList();
                    if(gradeList != null && !gradeList.isEmpty()){
                        starContainer.removeAllViews();
                        for (int i = 0; i<gradeList.size();i++){
                            GameScoreBean.Grade grade = gradeList.get(i);
                            ScorePorgressView scorePorgressView = new ScorePorgressView(GameScoreActivity.this, grade.getTitle(), StringUtils.toFloat(grade.getRatio(), 0.0f));
                            starContainer.addView(scorePorgressView);
                        }

                    }

                    loadView2.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            setTitleShow(false);
                            loadView2.showSuccess();
                        }
                    },200);
                }else{
                    loadView2.showErrorPage();
                }
            }
        };

        addPostCacheRequest(URIContant.GAME_GRADE_SCALE,new TypeToken<DataBean<GameScoreBean>>(){}.getType(),params,httpListener);


    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.error_page){
            loadData();
        }
    }
}
